package aimis.controller;


import aimis.domain.Monitor;
import aimis.dto.PageDTO;
import aimis.dto.PageQuery1;
import aimis.dto.Res;
import aimis.service.MonitorService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/api/monitor")
public class MonitorController {

    @Resource
    private MonitorService monitorService;

    /**
     * 查询所有数据(分页)
     * @return
     */
    @GetMapping("/page")
    public PageDTO<Monitor> queryUsersPage(PageQuery1 query){

        return monitorService.queryUsersPage(query);
    }
    /**
     * 查询所有数据
     * @return
     */

    @GetMapping("/test")
    public Res test(){
        List<Monitor> tobaccoList = monitorService.getAll();
        System.out.println(tobaccoList);
        return Res.success(tobaccoList,"获取成功");
    }
    @GetMapping
    public Res getAll2(){
        //return monitorService.getAll();
        List<Monitor> tobaccoList = monitorService.getAll();
        System.out.println(tobaccoList);
        //String msg = tobaccoList != null ? "查找全部成功" : "查找全部失败";
        //Integer code = tobaccoList != null ?200:404;
       // return  new Res(tobaccoList,msg);
        return Res.success(tobaccoList,"获取成功");
    }

    @GetMapping("/{date}")
    public List<Monitor> getByLikeName(@PathVariable String date) {
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDateTime localDateTime = localDate.atStartOfDay();
        return monitorService.getByLikeName(date);
    }

    /**
     * 添加数据
     * @param monitor
     * @return
     */
    @PostMapping
    public Res<Boolean> add(@RequestBody Monitor monitor){
        boolean flag = monitorService.save(monitor);
        return Res.success(flag,"添加成功");
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Res delete(@PathVariable String id){
        monitorService.removeById(id);
        return Res.success();
    }

    /**
     * 修改数据
     * @param monitor
     * @return
     */
    @PutMapping
    public Res update(@RequestBody Monitor monitor){
        monitorService.updateById(monitor);
        return Res.success();
    }







}
